package controller.SchoolRecruitManagement;

import entity.Recruitment;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import service.SchoolRecruitManagement.SchoolRecruitService;
import util.ResponseMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/SchoolRecruit")
public class SchoolRecruitController {

    @Autowired
    private SchoolRecruitService schoolRecruitService;

    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/show_recruitment")
    public ResponseMessage show_recruitment(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<LinkedHashMap<String,Object>> map = schoolRecruitService.select_recruitment(Current_logged_in_user.getUserId());
//        List<LinkedHashMap<String,Object>> map = schoolRecruitService.select_recruitment("13163357255");
        for (int i=0;i<map.size();i++){
            try {
                String dateString = String.valueOf(map.get(i).get("date"));
                map.get(i).put("beginTime",String.valueOf(map.get(i).get("beginTime")));
                map.get(i).put("endTime",String.valueOf(map.get(i).get("endTime")));
                map.get(i).put("date",dateString);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS,"获取成功",map);
    }


    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/show_myrecruitment")
    public ResponseMessage show_myrecruitment(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        List<LinkedHashMap<String,Object>> map = schoolRecruitService.select_myrecruitment(Current_logged_in_user.getUserId());
//        List<LinkedHashMap<String,Object>> map = schoolRecruitService.select_myrecruitment("13715131045");
        for (int i=0;i<map.size();i++){
            try {
//                String dateString = String.valueOf(map.get(i).get("date"));
                map.get(i).put("beginTime",String.valueOf(map.get(i).get("beginTime")));
                map.get(i).put("endTime",String.valueOf(map.get(i).get("endTime")));
                map.get(i).put("date",String.valueOf(map.get(i).get("date")));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResponseMessage(ResponseMessage.SUCCESS,"获取成功",map);
    }
        /**
         * @param  name（校招会标题，如华为2020春季校园招聘）
         * @param describe（校招会简要介绍）
         * @param beginTime（校招会时间，如2020年4月12日 14:00）
         * @param endTime   （校招会结束时间，如2020年4月12日 15:00）
         * @param address（校招会地点，如黄家湖校区10111）
         * @param school（校招面向学校，如武汉科技大学）
         * */
    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/issue_recruitment")
    public ResponseMessage issue_recruitment(String name,String describe,String beginTime,String endTime,String address,String school,String date,HttpServletRequest request) {
        HttpSession session = request.getSession();
        User Current_logged_in_user = (User) session.getAttribute("user");
        if (Current_logged_in_user == null) {
            return new ResponseMessage(ResponseMessage.ERROR, "用户未登录");
        }
        if (Current_logged_in_user.getType()!=3) {
            return new ResponseMessage(ResponseMessage.ERROR, "当前用户非企业用户，不能发布校招活动");
        }
        Recruitment recruitment = new Recruitment();
        recruitment.setEnterpriseId(schoolRecruitService.findEnterpriseId(Current_logged_in_user.getUserId()));
//        recruitment.setEnterpriseId(schoolRecruitService.findEnterpriseId("13715131045"));
        recruitment.setName(name);
        recruitment.setDescribe(describe);
        recruitment.setBeginTime(beginTime);
        recruitment.setEndTime(endTime);
        recruitment.setAddress(address);
        recruitment.setSchool(school);
        recruitment.setDate(date);
        int flag = schoolRecruitService.addRecruitment(recruitment);
        if (flag == 1){
            return new ResponseMessage(ResponseMessage.SUCCESS,"校招会发布成功",recruitment);
        }
        return new ResponseMessage(ResponseMessage.ERROR,"系统错误");
    }
}
